﻿using System;
using System.Web;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using Arch;
using InvoiceChecking.Data;
using InvoiceChecking.Data.SqlClient;
using InvoiceChecking.Entities;

public partial class Admin_admin : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //ApplyStyle();
        //if (Session["adminname"] == null)
        //    Response.Redirect("AdminLogin.aspx", true);
        //ECISaiGon.Entities.User obj = (ECISaiGon.Entities.User)Session["adminname"];
        //obj = DataRepository.UserProvider.GetByUserId(obj.UserId);
        //hpl_EditProfile.Text = obj.DisplayName;
        //hpl_EditProfile.NavigateUrl = "EditUser.aspx?uid=" + obj.UserId;
        //lbl_Name.Text = obj.DisplayName;
        //Page.ClientScript.RegisterClientScriptInclude(GetType(), "DisableNameActiveX", Page.ClientScript.GetWebResourceUrl(GetType(), "WebCommon.JS.CommonFunction.js"));

    }

    public void ApplyStyle(object sender, EventArgs eventArgs)
    {
        //Apply Theme
        var control = (HtmlControl)sender;
        var colorCode = control.ID;
        skinStyleSheet.Href = string.Format("../media/css/{0}.css", colorCode);
        form1.Attributes.Add("class", colorCode);
        //Save Theme to user profiles
        var authUserName = Context.User.Identity.Name.Split('\\')[1];
        var id = DataRepository.InvoiceCheckingUserProvider.GetByUserId(authUserName);
        id.Theme = colorCode;
        DataRepository.InvoiceCheckingUserProvider.Update(id);
    }

    protected void btnLogOut_Click(object sender, EventArgs eventArgs)
    {
        var httpCookie = Response.Cookies["ICConnectionString"];
        if (httpCookie != null)
            httpCookie.Expires = DateTime.Now.AddDays(-30);
        FormsAuthentication.RedirectToLoginPage();
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        HttpCookie httpCookie = Request.Cookies["ICConnectionString"];
        if (httpCookie != null)
        {
            SqlNetTiersProvider provider = GlobalUtilities.CreateProvider(Server.MapPath("~") + "/Config.xml",
                                                                          httpCookie.Value);
            if (provider != null)
                DataRepository.LoadProvider(provider, true);
            else
                btnLogOut_Click(null, null);
        }
        if (!IsPostBack)
        {
            string authUserName = Context.User.Identity.Name.Split('\\')[1];
            //HttpCookie conCookie = Request.Cookies["ICConnectionString"];
            if (httpCookie != null)
            {
                InvoiceCheckingUser id = DataRepository.InvoiceCheckingUserProvider.GetByUserId(authUserName);
                lbl_Server.Text = httpCookie.Value;
                hpl_EditProfile.Text = id.Fullname;
                var colorCode = id.Theme;
                if (!string.IsNullOrEmpty(colorCode))
                {
                    skinStyleSheet.Href = string.Format("../media/css/{0}.css", colorCode);
                    form1.Attributes.Add("class", colorCode);
                }
            }
            else
            {
                FormsAuthentication.RedirectToLoginPage();
            }
        }
    }
}
